class Solution {
    //非递归前序遍历
    
    public List<Integer> preorderTraversal(TreeNode root) {
      List<Integer> list = new ArrayList<>();
      if(root == null) return list;
       Stack<TreeNode> stack = new Stack<>() ;
       stack.push(root);
       while(!stack.empty()){
         TreeNode cur = stack.pop();
         list.add(cur.val);
          if(cur.right != null){
             stack.push(cur.right);
         }
         if(cur.left != null){
             stack.push(cur.left);
         }
       }
       return list;
    }
}